<?php

namespace app\admin\controller;


use think\App;
use app\admin\validate\Access as AccessValidate;
use app\admin\model\Access as AccessModel;


class Access extends Base
{
    protected $access = '';

    public function __construct(App $app, AccessModel $access)
    {
        parent::__construct($app);
        $this->access = $access;
    }

    /**
     * 权限列表
     */
    public function accessList(): string
    {
        if (isAjax()) {
            $type = input('request.type', '1');
            $page = intval(input('request.page', 1));
            $pageSize = intval(input('request.page_size', 15));
            $key = input('request.key', '');
            $pid = input('request.pid', '');
            $where = ['status' => 1];

            !empty($key) && $where['key'] = ['LIKE', '%' . $key . '%'];
            $pid !== '' && $where['pid'] = $pid;
            !empty($type) && $where['position_type'] = $type;

            $field = 'id,pid,name,key,url,menu,menu_icon icon,position_type type,create_time';
            $list = $this->access->where($where)->field($field)->paginate($pageSize);
            $parent_menu = $this->access->getParentMenu($type);
            ajaxSuccess(['list' => $list, 'parent_menu' => $parent_menu]);
        }
        return $this->fetch('access_list');
    }

    /**
     * 保存权限列表
     */
    public function accessSave(AccessValidate $accessValidate)
    {
        if (isAjax()) {
            $data = input('request.');
            if (!$accessValidate->check($data)) {
                ajaxError($accessValidate->getError());
            }
            $arr = [
                'pid' => $data['pid'],
                'name' => $data['name'],
                'key' => $data['key'],
                'url' => $data['url'],
                'menu' => $data['menu'],
                'menu_icon' => $data['icon'],
                'position_type' => empty($data['type']) ? 1 : $data['type'],
            ];
            if (empty($data['id'])) {
                $result = $this->access->save($arr);

            } else {
                $arr['id'] = $data['id'];
                $result = $this->access->update($arr);
            }
            if (!$result)
                ajaxError('保存失败');
            ajaxSuccess();
        }
    }

    /**
     * 删除权限节点
     */
    public function accessDel()
    {
        if (isAjax()) {
            $id = input('request.id');

            $item = $this->access->where(['id' => $id])->find();
            if (empty($item) || $item['status'] == -1) {
                ajaxError('数据不存在');
            }
            $item->status = -1;
            $item->save();

            if (empty($item['pid'])) {
                $this->access->update(['status' => -1], ['pid' => $id]);
            }

            ajaxSuccess();
        }
    }


    /**
     * 角色列表
     */
    public function roleList()
    {
        if (isAjax()) {
            $role = $this->access->getAuthList();
            ajaxSuccess($role);
        }
        return $this->fetch('role_list');
    }

    public function getRoleEmployee()
    {
        if (isAjax()) {
            $id = input('post.id');
            $page = input('post.page', 1, 'intval');
            $pageSize = input('post.pageSize', 15, 'intval');

            $where = [];
            $where['auth_id'] = $id;
            $result = $this->access->getRuleEmployee($where, $page, $pageSize);
            ajaxSuccess($result);
        }
    }

    /**
     * 角色权限
     */
    public function roleEdit()
    {
        if (isAjax()) {
            $postData = input('post.');
            empty($postData['name']) && ajaxError('角色名必须填写');
            if (!empty($postData['access']))
                $postData['access'] = json_decode($postData['access'], true);
            empty($postData['access']) && ajaxError('请选择权限');
            $data = [
                'id' => empty($postData['id']) ? '' : intval($postData['id']),
                'name' => $postData['name'],
                'status' => $postData['status'],
            ];
            $this->access->startTrans();
            $result = $this->access->saveAuth($data);
            if ($result) {
                $result = $this->access->saveAuthRole($result, $postData['access']);
            }
            if ($result !== false) {
                $this->access->commit();
                ajaxSuccess();
            }
            $this->access->rollback();
        }
        ajaxError('操作失败');
    }

    public function roleDel()
    {
        if (isAjax()) {
            $id = intval(input('post.id'));
            $result = $this->access->delAuth($id);
            if ($result['errCode']) {
                ajaxError($result['message']);
            }
            ajaxSuccess();
        }
    }

}

